package com.mzxy.mztopline.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.menu.MenuWrapperFactory;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.SyncStateContract;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itheima.PullToRefreshView;
import com.mzxy.mztopline.R;
import com.mzxy.mztopline.bean.PythonBean;
import com.mzxy.mztopline.framgent.HomeFragment;
import com.mzxy.mztopline.utils.Contsant;
import com.mzxy.mztopline.utils.JsonParse;
import com.mzxy.mztopline.view.WrapRecyclerView;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.lang.invoke.ConstantCallSite;
import java.security.PublicKey;
import java.util.List;

public class PythonActivity extends AppCompatActivity {

    private static final String TAG = PythonActivity.class.getName();
    private PullToRefreshView mPullToRefreshView;
    private WrapRecyclerView recycleView;
    public static final int REFRESH_DELAY = 1000;
    public static final int MSG_PYTHON_OK = 1; //获取数据
    private TextView tv_main_title, tv_back;
    private RelativeLayout rl_title_bar;
    private HomeFragment.MHandler mHandler;
    private Contsant Constant;
//    private PythonListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_home);
        initView();
        initData();
    }

    private void initView() {
        tv_main_title = (TextView) findViewById(R.id.tv_main_title);
        tv_main_title.setText("Python学科");
        rl_title_bar = (RelativeLayout) findViewById(R.id.title_bar);
        rl_title_bar.setBackgroundColor(getResources().getColor(
                R.color.rdTextColorPress));
        tv_back = (TextView) findViewById(R.id.tv_back);
        tv_back.setVisibility(View.VISIBLE);
        recycleView = (WrapRecyclerView) findViewById(R.id.recycler_view);
        recycleView.setLayoutManager(new LinearLayoutManager(this));

        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PythonActivity.this.finish();
            }
        });
    }

    private  void initData(){
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request =  new  Request.Builder().url(Constant.WEB_SITE+ Constant.REQUEST_PYTHON_URL).build();
            Call call = okHttpClient.newCall(request);
            //开启异步线程访问网络
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Request request, IOException e) {

                }

                @Override
                public void onResponse(Response response) throws IOException {
                    String res  =  response.body().string();
                    Message msg  =  new Message();
                    msg.what = MSG_PYTHON_OK;
                    msg.obj =res;
                    mHandler.sendMessage(msg);
                }
            });

        /**
         * 事件捕获
         */

        class  MHandler extends Handler{
            public void  dispatchMessage(Message msg ){
                super.dispatchMessage(msg);
                switch (msg.what){
                    case MSG_PYTHON_OK:
                        if (msg.obj != null){
                            String vlResult = (String) msg.obj;
                            //使用Gson解析
                            List<PythonBean>pythonList = JsonParse.getInstance().getPythonList(vlResult);

                            Log.d(TAG,"pythonList: "+(pythonList != null? pythonList.size():null));
                        }
                        break;
                }
            }
        }




    }
}
